import { _decorator, Component, Node, UITransform, Sprite, Animation, SpriteFrame, AnimationClip, animation } from 'cc'
const { ccclass, property } = _decorator

// 状态
import State from './State'
import { SubStateMchine } from './SubStateMchine'

// 状态机类型
import { FSM_PARMERS_TYPE_ENUM } from '../Enums'

export type pamersValueType = number | string | boolean

export interface PlayerState {
  type: FSM_PARMERS_TYPE_ENUM
  value: pamersValueType
}

// 设置初始值
export const getPlayerState = () => {
  return {
    type: FSM_PARMERS_TYPE_ENUM.TRIGGER, //触发器
    value: false,
  }
}

// 设置初始值
export const getPlayerDirection = () => {
  return {
    type: FSM_PARMERS_TYPE_ENUM.STRING, //字符串
    value: '',
  }
}

export const getInitParamsNumber = () => {
  return {
    type: FSM_PARMERS_TYPE_ENUM.NUMBER,
    value: 0,
  }
}

@ccclass('StateMchine')
export abstract class StateMchine extends Component {
  // 当前状态
  private _currentState: State | SubStateMchine | null = null

  // 参数
  pramers: Map<string, PlayerState> = new Map()

  // 状态机
  stateMachines: Map<string, State | SubStateMchine> = new Map()

  animationComponent: Animation | null = null

  waitingList: Array<Promise<SpriteFrame[]>> = [] //等待资源加载完成

  abstract init(): void
  abstract run(): void

  // 获取参数
  getPramers(type: string) {
    if (this.pramers.has(type)) {
      return this.pramers.get(type).value
    }
  }

  // 设置参数
  setPramers(type: string, value: pamersValueType) {
    if (this.pramers.has(type)) {
      this.pramers.get(type).value = value
      this.run()
      this.resetTrigger() // 重置所有的tigger
    }
  }

  // 重置所有的tigger
  resetTrigger() {
    this.pramers.forEach(item => {
      if (item.type === FSM_PARMERS_TYPE_ENUM.TRIGGER) {
        item.value = false
      }
    })
  }
  get currentState() {
    return this._currentState as State
  }

  set currentState(newState: State) {
    this._currentState = newState
    this._currentState?.run() //播放动画
  }
}
